<?php

namespace App\Backend\Http\Controllers;

use App\Data;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index(Request $request)
    {
        $data = $request->all();
        isset($data['name']) ? $where[] = ['name', 'like', '%' . $data['name'] . '%'] : '';
        isset($data['mobile']) ? $where[] = ['mobile', 'like', '%' . $data['mobile'] . '%'] : '';
        isset($data['is_active']) ? $where['is_active'] = $data['is_active'] : '';
        isset($data['is_vip']) ? $where['is_vip'] = $data['is_vip'] : '';
        isset($data['upmoney']) ? $where[] = ['upmoney', '>', 0] : '';
        isset($data['datanum']) ? $where[] = ['datanum', '>', 0] : '';
        !empty($data['order']) ? $order = $data['order'] : $order = 'created_at';
        $where[] = ['id', '>', 0];
        $data['users'] = User::where($where)->orderBy($order, 'desc')->paginate(20);
        if (isset($data['upmoney'])) {
            return view('backend::user.upmoneylist', $data);
        } else if (isset($data['datanum'])) {
            return view('backend::user.uplist', $data);
        }

        return view('backend::user.userlist', $data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('backend::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show($id)
    {
        $data = [
            'user' => User::find($id),
        ];
        return view('backend::user.userinfo', $data);
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('backend::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(Request $request)
    {
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        $user = User::find($id);
        $user->update(['is_active'=>0]);
        return redirect('backend/users');
    }
    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function usersuse($id)
    {
        $user = User::find($id);
        $user->update(['is_active'=>1]);
        return redirect('backend/users');
    }

    public function countuserdatas()
    {
        $users = User::get();
        foreach ($users as $user){
            $count = Data::where('user_id',$user->id)->count().'<br/>';
            $user->update(['datanum'=>$count]);
        }
        return redirect('backend/users?datanum=1');
    }
}
